package com.yekaile.carsystem.service;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.yekaile.carsystem.entity.Goods;
import com.yekaile.carsystem.entity.GoodsExample;
import com.yekaile.carsystem.entity.GoodsType;
import com.yekaile.carsystem.mapper.GoodsMapper;
import com.yekaile.carsystem.mapper.GoodsTypeMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author yekaile
 */
@Service
@AllArgsConstructor(onConstructor_=@Autowired)
public class GoodsServiceImpl implements GoodsService {


    GoodsMapper goodsMapper;

    GoodsTypeMapper goodsTypeMapper;



    @Override
    public List<Goods> getGoods(Goods goods) {
        if (goods == null) {
            return goodsMapper.selectAll();
        } else {
            GoodsExample example = new GoodsExample();
            if (goods.getGoodsId() != null) {
                example.or().andGoodsIdEqualTo(goods.getGoodsId());
            }
            if (!StrUtil.hasBlank(goods.getGoodsName())) {
                example.or().andGoodsNameLike("%".concat(goods.getGoodsName().concat("%")));
            }
            if (!StrUtil.hasBlank(goods.getGoodsTypeName())) {
                example.or().andGoodsTypeNameLike("%".concat(goods.getGoodsTypeName().concat("%")));
            }
            if (goods.getIsDeleted() != null) {
                example.or().andIsDeletedEqualTo(goods.getIsDeleted());
            }

            return goodsMapper.selectByExample(example);
        }
    }

    @Override
    public Integer doGoods(Integer goodsId, Integer isDeleted) {
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        goods.setIsDeleted(isDeleted);
        goods.setGmtModified(DateUtil.now());
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public Integer addGoods(Goods goods) {
        return goodsMapper.insertSelective(goods);
    }

    @Override
    public List<GoodsType> listGoodsType(Integer goodsTypeId) {
        if(goodsTypeId!=null){
            return Collections.singletonList(goodsTypeMapper.selectByPrimaryKey(goodsTypeId));
        }
        return goodsTypeMapper.selectAll();
    }

    @Override
    public Integer updateGoods(Goods goods) {
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public Integer doGoodsType(Integer goodsTypeId, Integer isDeleted) {
        GoodsType goodsType= goodsTypeMapper.selectByPrimaryKey(goodsTypeId);
        goodsType.setIsDeleted(isDeleted);
        return goodsTypeMapper.updateByPrimaryKeySelective(goodsType);
    }

    @Override
    public Integer addGoodsType(GoodsType goodsType) {
        return goodsTypeMapper.insertSelective(goodsType);
    }

    @Override
    public Integer updateGoodsType(GoodsType goodsType) {
        return goodsTypeMapper.updateByPrimaryKeySelective(goodsType);
    }


}
